package com.eascs.web.o2o.scheduler.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.scheduler.dao.ServiceDao;

@Service
public class SynoInfoAssetStructureService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	ServiceDao serviceDao;

	/**
	 * @Title: saveAssetStructurePrincipalDaily
	 * @Description: report-10175 资产结构本金日报 (数据入库操作)
	 * @author GYH
	 * @return void
	 * @throws @date
	 *             2017年05月26日 上午9:43:18
	 */
	public void saveAssetStructurePrincipalDaily() {
		logger.info("[Service库]资产结构本金日报表数据同步开始.........");

		logger.info("【删除】产结构本金日报表数据开始-------------------------------------");
		String deleteSql = "sql.deleteSql";
		serviceDao.excuteSql(deleteSql);
		logger.info("【删除】产结构本金日报表数据结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】贷款单号，贷款id，省区
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓----------------------------------------------------

		logger.info("【插入】贷款单号，贷款id，省区字段开始-------------------------------------");
		String insertSql = "sql.insertSql";
		try {
			serviceDao.excuteSql(insertSql);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("错误:保存【贷款单号，贷款id，省区】数据库操作错误");
		}

		logger.info("【插入】贷款单号，贷款id，省区字段结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】全部贷款余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓------------------------------------------------------------

		// 查询资产结构本金日报表中的全部bill_code
		logger.info("【插入】全部贷款余额开始-------------------------------------");
		getLoanBalanceByBillCode();
		logger.info("【插入】全部贷款余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额合计
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------------

		logger.info("【插入】未到期余额合计开始-------------------------------------");
		getTotalAmountNdbByBillCode();
		logger.info("【插入】未到期余额合计结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额正常还息余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额正常还息余额开始-------------------------------------");
		getNormalAlsoBalanceNdb();
		logger.info("【插入】未到期余额正常还息余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额利息逾期余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额利息逾期余额开始-------------------------------------");
		getInterestDueBalanceNdb();
		logger.info("【插入】未到期余额利息逾期余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额利息代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额利息代偿余额开始-------------------------------------");
		getInterestDCBalanceNdb();
		logger.info("【插入】未到期余额利息代偿余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额到期未代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额到期未代偿余额开始-------------------------------------");
		getExpireNotdcBalanceEub();
		logger.info("【插入】已到期未结清余额到期未代偿余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额代偿余额开始-------------------------------------");
		getDCBalanceEub();
		logger.info("【插入】已到期未结清余额代偿余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额本年代偿回款金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额本年代偿回款金额开始-------------------------------------");
		getYearDCReceivableEub();
		logger.info("【插入】已到期未结清余额本年代偿回款金额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额本年代偿金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额本年代偿金额开始-------------------------------------");
		getYearDCAmountEub();
		logger.info("【插入】已到期未结清余额本年代偿金额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额历年累计代偿金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额历年累计代偿金额开始-------------------------------------");
		getHistoryDCAmountEub();
		logger.info("【插入】已到期未结清余额历年累计代偿金额结束-------------------------------------");

		logger.info("[Service库]资产结构本金日报表数据同步结束.........");
	}

	// 根据bill_code查询【全部贷款余额】
	public void getLoanBalanceByBillCode() {
		String queryLoanWfTypeSql = "sql.queryLoanWfType";
		String updateBalanceByBillCodeSql = "sql.updateBalanceByBillCode";
		// 逾期状态下
		String queryYQBalanceSql = "sql.queryYQBalance";
		List<Object[]> YQbalance = serviceDao.findObjectsList(queryYQBalanceSql);
		if (YQbalance.size() != 0) {
			for (Object[] yQbalance : YQbalance) {
				List<String> wfType = serviceDao.findSimpleObjectList(queryLoanWfTypeSql, String.class, yQbalance[0]);
				if ("YQ".equals(wfType.get(0))) {
					try {
						// 根据bill_code修改全部贷款余额
						serviceDao.excuteSql(updateBalanceByBillCodeSql, yQbalance[1], yQbalance[0]);
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("错误:修改【全部贷款余额】数据库操作错误，贷款单号：[{}]", yQbalance[0]);
					}
				}
			}
		}
		// 代偿状态下
		String queryDCBalanceSql = "sql.queryDCBalance";
		List<Object[]> DCbalance = serviceDao.findObjectsList(queryDCBalanceSql);
		if (DCbalance.size() != 0) {
			for (Object[] dCbalance : DCbalance) {
				List<String> wfType = serviceDao.findSimpleObjectList(queryLoanWfTypeSql, String.class, dCbalance[0]);
				if ("DC".equals(wfType.get(0))) {
					try {
						// 根据bill_code修改全部贷款余额
						serviceDao.excuteSql(updateBalanceByBillCodeSql, dCbalance[1], dCbalance[0]);
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("错误:修改【全部贷款余额】数据库操作错误，贷款单号：[{}]", dCbalance[0]);
					}
				}
			}
		}
		// 正常还款状态下
		String queryOBalanceSql = "sql.queryOBalance";
		List<Object[]> Obalance = serviceDao.findObjectsList(queryOBalanceSql);
		if (Obalance.size() != 0) {
			for (Object[] obalance : Obalance) {
				List<String> wfType = serviceDao.findSimpleObjectList(queryLoanWfTypeSql, String.class, obalance[0]);
				if ("O".equals(wfType.get(0))) {
					try {
						// 根据bill_code修改全部贷款余额
						serviceDao.excuteSql(updateBalanceByBillCodeSql, obalance[1], obalance[0]);
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("错误:修改【全部贷款余额】数据库操作错误，贷款单号：[{}]", obalance[0]);
					}
				}
			}
		}
	}

	// 根据bill_code查询【未到期余额合计】
	public void getTotalAmountNdbByBillCode() {
		String updateTotalAmountNdbSql = "sql.updateTotalAmountNdb";
		String queryTotalAmountNdbSql = "sql.queryTotalAmountNdb";
		List<Object[]> TotalAmount = serviceDao.findObjectsList(queryTotalAmountNdbSql);
		if (TotalAmount.size() != 0) {
			for (Object[] totalAmount : TotalAmount) {
				try {
					// 根据bill_code修改未到期余额合计
					serviceDao.excuteSql(updateTotalAmountNdbSql, totalAmount[1], totalAmount[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【未到期余额合计】数据库操作错误，贷款单号：[{}]", totalAmount[0]);
				}
			}
		}
	}

	// 根据bill_code查询【未到期余额正常还息余额】
	public void getNormalAlsoBalanceNdb() {
		String updateNormalAlsoBalanceSql = "sql.updateNormalAlsoBalanceNdb";
		String queryNormalAlsoBalanceNdbSql = "sql.queryNormalAlsoBalanceNdb";
		List<Object[]> NormalAlsoBalance = serviceDao.findObjectsList(queryNormalAlsoBalanceNdbSql);
		if (NormalAlsoBalance.size() != 0) {
			for (Object[] normalAlsoBalance : NormalAlsoBalance) {
				try {
					// 根据bill_code修改未到期余额正常还息余额
					serviceDao.excuteSql(updateNormalAlsoBalanceSql, normalAlsoBalance[1], normalAlsoBalance[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【未到期余额正常还息余额】数据库操作错误，贷款单号：[{}]", normalAlsoBalance[0]);
				}

			}
		}
	}

	// 根据bill_code查询【未到期余额利息逾期余额】
	public void getInterestDueBalanceNdb() {
		String updateInterestDueBalanceNdbSql = "sql.updateInterestDueBalanceNdb";
		String queryInterestDueBalanceNdbSql = "sql.queryInterestDueBalanceNdb";
		List<Object[]> InterestDueBalance = serviceDao.findObjectsList(queryInterestDueBalanceNdbSql);
		if (InterestDueBalance.size() != 0) {
			for (Object[] interestDueBalance : InterestDueBalance) {
				try {
					// 根据bill_code修改未到期余额利息逾期余额
					serviceDao.excuteSql(updateInterestDueBalanceNdbSql, interestDueBalance[1], interestDueBalance[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【未到期余额利息逾期余额】数据库操作错误，贷款单号：[{}]", interestDueBalance[0]);
				}
			}
		}
	}

	// 根据bill_code查询【未到期余额利息代偿余额】
	public void getInterestDCBalanceNdb() {
		String updateInterestDCBalanceNdbSql = "sql.updateInterestDCBalanceNdb";
		String queryInterestDCBalanceNdbSql = "sql.queryInterestDCBalanceNdb";
		List<Object[]> InterestDCBalance = serviceDao.findObjectsList(queryInterestDCBalanceNdbSql);
		if (InterestDCBalance.size() != 0) {
			for (Object[] interestDCBalance : InterestDCBalance) {
				try {
					// 根据bill_code修改未到期余额利息代偿余额
					serviceDao.excuteSql(updateInterestDCBalanceNdbSql, interestDCBalance[1], interestDCBalance[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【未到期余额利息代偿余额】数据库操作错误，贷款单号：[{}]", interestDCBalance[0]);
				}
			}
		}
	}

	// 根据bill_code查询【已到期未结清余额到期未代偿余额】
	public void getExpireNotdcBalanceEub() {
		String updateExpireNotdcBalanceEubSql = "sql.updateExpireNotdcBalanceEub";
		String queryExpireNotdcBalanceEubSql = "sql.queryExpireNotdcBalanceEub";
		List<Object[]> ExpireNotdcBalance = serviceDao.findObjectsList(queryExpireNotdcBalanceEubSql);
		if (ExpireNotdcBalance.size() != 0) {
			for (Object[] expireNotdcBalance : ExpireNotdcBalance) {
				try {
					// 根据bill_code修改已到期未结清余额到期未代偿余额
					serviceDao.excuteSql(updateExpireNotdcBalanceEubSql, expireNotdcBalance[1], expireNotdcBalance[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【已到期未结清余额到期未代偿余额】数据库操作错误，贷款单号：[{}]", expireNotdcBalance[0]);
				}
			}
		}
	}

	// 根据bill_code查询【已到期未结清余额代偿余额】
	public void getDCBalanceEub() {
		String updateDCBalanceEubSql = "sql.updateDCBalanceEub";
		String queryDCBalanceEubSql = "sql.queryDCBalanceEub";
		List<Object[]> DCBalance = serviceDao.findObjectsList(queryDCBalanceEubSql);
		if (DCBalance.size() != 0) {
			for (Object[] dCBalance : DCBalance) {
				try {
					// 根据bill_code修改已到期未结清余额代偿余额
					serviceDao.excuteSql(updateDCBalanceEubSql, dCBalance[1], dCBalance[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【已到期未结清余额代偿余额】数据库操作错误，贷款单号：[{}]", dCBalance[0]);
				}
			}
		}
	}

	// 根据bill_code查询【已到期未结清余额本年代偿回款金额】
	public void getYearDCReceivableEub() {
		String updateYearDCReceivableEubSql = "sql.updateYearDCReceivableEub";
		String queryYearDCReceivableEubSql = "sql.queryYearDCReceivableEub";
		List<Object[]> YearDCReceivable = serviceDao.findObjectsList(queryYearDCReceivableEubSql);
		if (YearDCReceivable.size() != 0) {
			for (Object[] yearDCReceivable : YearDCReceivable) {
				try {
					// 根据bill_code修改已到期未结清余额本年代偿回款金额
					serviceDao.excuteSql(updateYearDCReceivableEubSql, yearDCReceivable[1], yearDCReceivable[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【已到期未结清余额本年代偿回款金额】数据库操作错误，贷款单号：[{}]", yearDCReceivable[0]);
				}
			}
		}
	}

	// 根据bill_code查询【已到期未结清余额本年代偿金额】
	public void getYearDCAmountEub() {
		String updateYearDCAmountEubSql = "sql.updateYearDCAmountEub";
		String queryYearDCAmountEubSql = "sql.queryYearDCAmountEub";
		List<Object[]> YearDCAmount = serviceDao.findObjectsList(queryYearDCAmountEubSql);
		if (YearDCAmount.size() != 0) {
			for (Object[] yearDCAmount : YearDCAmount) {
				try {
					// 根据bill_code修改已到期未结清余额本年代偿金额
					serviceDao.excuteSql(updateYearDCAmountEubSql, yearDCAmount[1], yearDCAmount[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【已到期未结清余额本年代偿金额】数据库操作错误，贷款单号：[{}]", yearDCAmount[0]);
				}
			}
		}
	}

	// 根据bill_code查询【已到期未结清余额历年累计代偿金额】
	public void getHistoryDCAmountEub() {
		String updateHistoryDCAmountEubSql = "sql.updateHistoryDCAmountEub";
		String queryHistoryDCAmountEubSql = "sql.queryHistoryDCAmountEub";
		List<Object[]> HistoryDCAmount = serviceDao.findObjectsList(queryHistoryDCAmountEubSql);
		if (HistoryDCAmount.size() != 0) {
			for (Object[] historyDCAmount : HistoryDCAmount) {
				try {
					// 根据bill_code修改已到期未结清余额历年累计代偿金额
					serviceDao.excuteSql(updateHistoryDCAmountEubSql, historyDCAmount[1], historyDCAmount[0]);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("错误:修改【已到期未结清余额历年累计代偿金额】数据库操作错误，贷款单号：[{}]", historyDCAmount[0]);
				}
			}
		}
	}
}
